package com.yubest.demo.handler;

import com.yubest.demo.handler.impl.NonHandler;
import com.yubest.demo.handler.impl.RarHandler;
import com.yubest.demo.handler.impl.SevenZHandler;
import com.yubest.demo.handler.impl.ZipHandler;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @Author hweiyu
 * @Description
 * @Date 2021/8/12 9:38
 */
@Getter
@AllArgsConstructor
public enum FileType {

    UN_KNOW("un_know", NonHandler.class),

    ZIP(".zip", ZipHandler.class),

    SEVEN_Z(".7z", SevenZHandler.class),

    RAR(".rar", RarHandler.class),


    ;

    private String type;

    private Class<? extends Handler> cls;

    public static FileType of(String type) {
        if (null != type) {
            for (FileType cur : values()) {
                if (type.toLowerCase().endsWith(cur.getType())) {
                    return cur;
                }
            }
        }
        return UN_KNOW;
    }

    public Handler getHandler() {
        try {
            return this.getCls().newInstance();
        } catch (Exception e) {
            return new NonHandler();
        }
    }
}
